package com.groupbuying.orderproducer.service.impl;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.shade.com.alibaba.fastjson.JSON;
import com.aliyun.openservices.shade.com.alibaba.fastjson.serializer.SerializerFeature;
import com.groupbuying.orderproducer.connector.StoreFeign;
import com.groupbuying.orderproducer.connector.SysConfigFeign;
import com.groupbuying.orderproducer.constants.CommonConstants;
import com.groupbuying.orderproducer.constants.OrderUshopEnumConstants;
import com.groupbuying.orderproducer.context.FilterContextHandler;
import com.groupbuying.orderproducer.dao.CommodityOrderMainDao;
import com.groupbuying.orderproducer.dao.OrderUshopDao;
import com.groupbuying.orderproducer.domain.CommodityOrderMainDO;
import com.groupbuying.orderproducer.domain.OrderUshopDO;
import com.groupbuying.orderproducer.dto.OrderUshopApiDTO;
import com.groupbuying.orderproducer.dto.OrderUshopDTO;
import com.groupbuying.orderproducer.mq.OrderMQConfig;
import com.groupbuying.orderproducer.service.OrderUshopService;
import com.groupbuying.orderproducer.utils.BeanOrMapConvertUtils;
import com.groupbuying.orderproducer.utils.DateUtils;
import com.groupbuying.orderproducer.utils.PageUtils;
import com.groupbuying.orderproducer.utils.Query;
import com.groupbuying.orderproducer.utils.apiresult.ApiResult;
import com.groupbuying.orderproducer.utils.apiresult.ApiResultCode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderUshopServiceImpl implements OrderUshopService {
    public static final Logger LOGGER = LoggerFactory.getLogger(OrderUshopServiceImpl.class);

    @Autowired
    private OrderUshopDao orderUshopDao;

    @Autowired
    private SysConfigFeign sysConfigFeign;

    @Autowired
    private StoreFeign storeFeign;

    @Autowired
    private Producer producer;

    @Autowired
    private CommodityOrderMainDao commodityOrderMainDao;



    @Override
    public OrderUshopDO get(String orderId) {
        return orderUshopDao.get(orderId);
    }

    @Override
    public List<OrderUshopDO> list(Map<String, Object> map) {
        return orderUshopDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return orderUshopDao.count(map);
    }

    @Override
    public int save(OrderUshopDO orderUshop) {
        return orderUshopDao.save(orderUshop);
    }

    @Override
    public int update(OrderUshopDO orderUshop) {
        return orderUshopDao.update(orderUshop);
    }

    @Override
    public int remove(String orderId) {
        return orderUshopDao.remove(orderId);
    }

    @Override
    public int batchRemove(String[] orderIds) {
        return orderUshopDao.batchRemove(orderIds);
    }

    /**
     * 优店订单
     *
     * @param mapParams
     * @return com.groupbuying.orderproducer.utils.apiresult.ApiResult
     * @Author Vincentxin
     * @Date 2019/8/27 17:13
     **/
    @Override
    public ApiResult createOrder(Map<String, Object> mapParams) {
        // 解析map参数
        //收银台ID
        LOGGER.info("===========START-----够样优店创建订单,收银台ID:{}，优惠规则ID:{},订单金额:{}", mapParams.get("counterId"), mapParams.get("opxRuleId"), mapParams.get("totalPrice"));
        Object tempParam = mapParams.get("counterId");
        String counterUuid = tempParam == null ? null : tempParam.toString();
        tempParam = mapParams.get("opxRuleId");
        String opxRuleId = tempParam == null ? null : tempParam.toString();
        tempParam = mapParams.get("totalPrice");
        String totalPrice = tempParam == null ? null : tempParam.toString();
        //判断请求参数不为空
        if (StringUtils.isEmpty(counterUuid) || StringUtils.isEmpty(opxRuleId) || StringUtils.isEmpty(totalPrice)) {
            LOGGER.error("够样优店创建订单,参数为空!收银台ID:{},优惠规则:{},订单金额{}", counterUuid, opxRuleId, totalPrice);
            return ApiResult.paramIsNull();
        }
        Map<String, Object> param = new HashMap<>();
        param.put("counterUuid", counterUuid);
        LOGGER.info("够样优店验证收银台ID:{}", counterUuid);
        ApiResult result = storeFeign.checkCounter(param);
        if (ApiResultCode.DATA_INVALID.code().equals(result.getCode())) {
            LOGGER.error("够样优店验证收银台ID:{},不可使用。", counterUuid);
            return ApiResult.failure("收银台不存在");
        }

        //生成订单信息
        OrderUshopDO orderUshopDO = new OrderUshopDO();
        orderUshopDO.setOrderId(sysConfigFeign.getOrderID(mapParams.get("cityCode").toString(), mapParams.get("bizCode").toString()));
        orderUshopDO.setIsDelete(CommonConstants.COMMON_FLAG_UNDEL);
        orderUshopDO.setGmtCreate(new Date());
        orderUshopDO.setGmtModified(new Date());
        orderUshopDO.setOrderState(OrderUshopEnumConstants.ENUM_ORDERSTATE_UNPAY);
        orderUshopDO.setOpxRuleId(opxRuleId);
        orderUshopDO.setCounterId(counterUuid);
        orderUshopDO.setDealState(OrderUshopEnumConstants.ENUM_DEALSTATE_UNCLOSED);
        orderUshopDO.setCustomerId(FilterContextHandler.getUserID());
        orderUshopDO.setOrderPrice(new BigDecimal(totalPrice));
        //设置订单关闭时间
        orderUshopDO.setClosedTime(new Date(orderUshopDO.getGmtCreate().getTime() + CommonConstants.ORDER_OVERDUE));
        this.save(orderUshopDO);
        LOGGER.info("够样优店订单保存成功，收银台ID:{},订单号:{}", orderUshopDO.getCounterId(), orderUshopDO.getOrderId());
        try {
            Message message = new Message(OrderMQConfig.TOPIC, OrderMQConfig.DEAL_TAG_TIMINGCLOSE,
                    JSON.toJSONString(BeanOrMapConvertUtils.objectToMap(orderUshopDO), SerializerFeature.DisableCircularReferenceDetect).getBytes());
            message.setStartDeliverTime(orderUshopDO.getClosedTime().getTime());
            producer.send(message);
            LOGGER.info("够样优店订单定时检测发送MQ成功，订单号:{}", orderUshopDO.getOrderId());
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info("够样优店订单定时检测发送MQ失败，订单号:{}", orderUshopDO.getOrderId());
        }

        //商户积分规则,计算积分实付金额及现金支付金额
        LOGGER.info("够样优店订单:{}查询优惠规则", orderUshopDO.getOrderId());
        Map ruleMap = new HashMap();
        ruleMap.put("ruleId", opxRuleId);
        ruleMap.put("delFlag", CommonConstants.COMMON_FLAG_UNDEL);
        Map ruleRes = sysConfigFeign.getStoreOpexRule(ruleMap);
        OrderUshopApiDTO orderUshopApiDTO = new OrderUshopApiDTO();
        orderUshopApiDTO.setOrderId(orderUshopDO.getOrderId());
        if (ruleRes != null) {
            BigDecimal discount = new BigDecimal(ruleRes.get("currencyPayDiscount").toString());
            discount = discount.multiply(new BigDecimal(totalPrice)).divide(new BigDecimal("100"));
            orderUshopApiDTO.setPayCurrency(discount);
            orderUshopApiDTO.setPayCash(orderUshopDO.getOrderPrice());
        }
        LOGGER.info("===========END-----够样优店订单创建成功并返回。订单ID:{},收银台ID:{}", orderUshopDO.getOrderId(), orderUshopDO.getCounterId());
        return ApiResult.success(orderUshopApiDTO);
    }
    /**
     * 优惠买单订单
     *
     * @Author Vincentxin
     * @Date 2019/8/27 17:14
     * @param map
     * @return com.groupbuying.orderproducer.utils.apiresult.ApiResult
     **/
    @Override
    public ApiResult createOrderForDiscounts(Map<String, Object> map) {
        //收银台ID
        LOGGER.info("===========START-----优惠买单创建订单,收银台ID:{},订单金额:{}", map.get("counterId"), map.get("totalPrice"));
        Object tempParam = map.get("counterId");
        String counterUuid = tempParam == null ? null : tempParam.toString();
        String opxRuleId = "优惠买单";
        tempParam = map.get("totalPrice");
        String totalPrice = tempParam == null ? null : tempParam.toString();


        //判断请求参数不为空
        if (StringUtils.isEmpty(counterUuid) || StringUtils.isEmpty(opxRuleId) || StringUtils.isEmpty(totalPrice)) {
            LOGGER.error("优惠买单创建订单,参数为空!收银台ID:{},订单金额{}", counterUuid, totalPrice);
            return ApiResult.paramIsNull();
        }
        Map<String, Object> param = new HashMap<>();
        param.put("counterUuid", counterUuid);
        LOGGER.info("优惠买单验证收银台ID:{}", counterUuid);
        ApiResult result = storeFeign.checkCounter(param);
        if (ApiResultCode.DATA_INVALID.code().equals(result.getCode())) {
            LOGGER.error("优惠买单验证收银台ID:{},不可使用。", counterUuid);
            return ApiResult.failure("收银台不存在");
        }
        //生成订单信息
        OrderUshopDO orderUshopDO = new OrderUshopDO();
        orderUshopDO.setOrderId(sysConfigFeign.getOrderID(map.get("cityCode").toString(), map.get("bizCode").toString()));
        orderUshopDO.setIsDelete(CommonConstants.COMMON_FLAG_UNDEL);
        orderUshopDO.setGmtCreate(new Date());
        orderUshopDO.setGmtModified(new Date());
        orderUshopDO.setOrderState(OrderUshopEnumConstants.ENUM_ORDERSTATE_UNPAY);
        orderUshopDO.setOpxRuleId(opxRuleId);
        orderUshopDO.setCounterId(counterUuid);
        orderUshopDO.setDealState(OrderUshopEnumConstants.ENUM_DEALSTATE_UNCLOSED);
        orderUshopDO.setCustomerId(FilterContextHandler.getUserID());
        orderUshopDO.setOrderPrice(new BigDecimal(totalPrice));
        //设置订单关闭时间
        orderUshopDO.setClosedTime(new Date(orderUshopDO.getGmtCreate().getTime() + CommonConstants.ORDER_OVERDUE));
        this.save(orderUshopDO);
        LOGGER.info("优惠买单订单保存成功，收银台ID:{},订单号:{}", orderUshopDO.getCounterId(), orderUshopDO.getOrderId());
        try {
            Message message = new Message(OrderMQConfig.TOPIC, OrderMQConfig.DEAL_TAG_TIMINGCLOSE,
                    JSON.toJSONString(BeanOrMapConvertUtils.objectToMap(orderUshopDO), SerializerFeature.DisableCircularReferenceDetect).getBytes());
            message.setStartDeliverTime(orderUshopDO.getClosedTime().getTime());
            producer.send(message);
            LOGGER.info("优惠买单订单定时检测发送MQ成功，订单号:{}", orderUshopDO.getOrderId());
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info("优惠买单订单定时检测发送MQ失败，订单号:{}", orderUshopDO.getOrderId());
        }
        OrderUshopApiDTO orderUshopApiDTO = new OrderUshopApiDTO();
        orderUshopApiDTO.setOrderId(orderUshopDO.getOrderId());
        orderUshopApiDTO.setPayCash(orderUshopDO.getOrderPrice());
        LOGGER.info("===========END-----优惠买单订单创建成功并返回。订单ID:{},收银台ID:{}", orderUshopDO.getOrderId(), orderUshopDO.getCounterId());
        return ApiResult.success(orderUshopApiDTO);
    }

    /**
     * 通过订单ID获取订单
     *
     * @param orderId 订单编号
     * @return
     */
    @Override
    public Map getOrder(String orderId) {
        if (StringUtils.isEmpty(orderId)) {
            return ApiResult.paramIsNull();
        }
        if ("002".equals(orderId.substring(4,7))){
            CommodityOrderMainDO commodityOrderMainDO = commodityOrderMainDao.get(orderId);

            if (commodityOrderMainDO == null) {
                LOGGER.error("够样优店商品订单查询订单信息不存在！");
                return ApiResult.failure("订单信息不存在");
            }
            Map resultMap = new HashMap();
            try {
               resultMap.put("orderPrice",commodityOrderMainDO.getPresentPrice());
                LOGGER.info("获取商品订单订单成功,订单号:{}", commodityOrderMainDO.getId());
            } catch (Exception e) {
                LOGGER.error("够样优店商品订单查询订单信息返回失败");
                e.printStackTrace();
            }
            return resultMap;
        }
        OrderUshopDO orderUshopDO = orderUshopDao.get(orderId);
        if (orderUshopDO == null) {
            return ApiResult.failure("订单信息不存在");
        }
        Map resultMap = new HashMap();
        try {
            resultMap = BeanOrMapConvertUtils.objectToMap(orderUshopDO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 通过订单ID获取订单及优惠规则信息
     *
     * @param orderId 订单编号
     * @return
     */
    @Override
    public Map getOrderAndOpexRule(String orderId) {
        if (StringUtils.isEmpty(orderId)) {
            return ApiResult.paramIsNull();
        }
        OrderUshopDO orderUshopDO = orderUshopDao.get(orderId);
        if (orderUshopDO == null) {
            return ApiResult.failure("订单信息不存在");
        }
        Map ruleMap = new HashMap();
        ruleMap.put("ruleId", orderUshopDO.getOpxRuleId());
        ruleMap.put("delFlag", CommonConstants.COMMON_FLAG_UNDEL);
        Map ruleRes = sysConfigFeign.getStoreOpexRule(ruleMap);
        Map resultMap = new HashMap();
        try {
            resultMap = BeanOrMapConvertUtils.objectToMap(orderUshopDO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        resultMap.putAll(ruleRes);

        return resultMap;
    }

    /**
     * 修改交易状态
     *
     * @param orderId   订单ID
     * @param dealState 交易状态
     * @return
     */
    @Override
    public ApiResult updateDealState(String orderId, String dealState) {
        if (StringUtils.isEmpty(orderId) && StringUtils.isEmpty(dealState)) {
            return ApiResult.failure("参数不能为空");
        }
        OrderUshopDO orderUshopDO = orderUshopDao.get(orderId);
        //数据库存在数据
        if (orderUshopDO != null) {
            if (dealState.equals(OrderUshopEnumConstants.ENUM_DEALSTATE_UNCLOSED) &&
                    !OrderUshopEnumConstants.ENUM_ORDERSTATE_UNPAY.equals(orderUshopDO.getOrderState())) {
                //订单完成
                orderUshopDO.setStateNote(OrderUshopEnumConstants.ENUM_STATENOTE_FINISH);
            }
            orderUshopDO.setGmtModified(new Date());
            orderUshopDO.setDealState(dealState);
            this.update(orderUshopDO);
            return ApiResult.success();
        }
        return ApiResult.failure("订单不存在");
    }

    /**
     * 关闭订单
     *
     * @param orderId   订单ID
     * @param closeType 关闭操作类型
     * @return
     */
    @Override
    public ApiResult closeDealState(String orderId, String closeType) {
        if (StringUtils.isEmpty(orderId)) {
            return ApiResult.failure("参数为空");
        }
        OrderUshopDO orderUshopDO = orderUshopDao.get(orderId);
        //存在数据且订单状态为未支付，交易状态不是已关闭
        if (orderUshopDO != null) {
            if (OrderUshopEnumConstants.ENUM_ORDERSTATE_UNPAY.equals(orderUshopDO.getOrderState()) &&
                    !OrderUshopEnumConstants.ENUM_DEALSTATE_CLOSED.equals(orderUshopDO.getDealState())) {
                if (CommonConstants.AUTO_OPERATION.equals(closeType)) {
                    orderUshopDO.setDealState(OrderUshopEnumConstants.ENUM_DEALSTATE_CLOSED);
                    orderUshopDO.setStateNote(OrderUshopEnumConstants.ENUM_STATENOTE_TIMEOUT);
                    //TODO 支付中心查询支付状态是否有支付完成的情况
                } else if (CommonConstants.USER_OPERATION.equals(closeType)) {
                    orderUshopDO.setDealState(OrderUshopEnumConstants.ENUM_DEALSTATE_CLOSED);
                    orderUshopDO.setStateNote(OrderUshopEnumConstants.ENUM_STATENOTE_CANCEL);
                }
                orderUshopDO.setGmtModified(new Date());
                this.update(orderUshopDO);
            }

            return ApiResult.success();
        }
        return ApiResult.failure("订单不存在");
    }

    /**
     * 修改订单状态
     *
     * @param orderId    订单id
     * @param orderState 订单状态
     * @return
     */
    @Override
    public ApiResult updateOrderState(String orderId, String orderState) {
        if (StringUtils.isEmpty(orderId) && StringUtils.isEmpty(orderState)) {
            return ApiResult.failure("参数不能为空");
        }
        OrderUshopDO orderUshopDO = orderUshopDao.get(orderId);
        //数据库存在数据且交易状态为进行中
        if (orderUshopDO != null && OrderUshopEnumConstants.ENUM_DEALSTATE_UNCLOSED.equals(orderUshopDO.getDealState())) {
            //如果修改订单状态为退款成功
            if (OrderUshopEnumConstants.ENUM_ORDERSTATE_REFUNDED.equals(orderState)) {
                //修改交易状态为关闭，并修改状态备注为退款完成交易关闭
                orderUshopDO.setDealState(OrderUshopEnumConstants.ENUM_DEALSTATE_CLOSED);
                orderUshopDO.setStateNote(OrderUshopEnumConstants.ENUM_STATENOTE_RETURN);
            }
            orderUshopDO.setGmtModified(new Date());
            orderUshopDO.setOrderState(orderState);
            this.update(orderUshopDO);
            return ApiResult.success();
            //交易状态关闭且是自动关闭的状态,再收到成功的通知时修改订单状态
        }else if (orderUshopDO != null &&
                OrderUshopEnumConstants.ENUM_ORDERSTATE_PAY.equals(orderState) &&
                OrderUshopEnumConstants.ENUM_DEALSTATE_CLOSED.equals(orderUshopDO.getDealState()) &&
                OrderUshopEnumConstants.ENUM_STATENOTE_TIMEOUT.equals(orderUshopDO.getStateNote())){
            orderUshopDO.setDealState(OrderUshopEnumConstants.ENUM_DEALSTATE_UNCLOSED);
            orderUshopDO.setStateNote(null);
            orderUshopDO.setGmtModified(new Date());
            orderUshopDO.setOrderState(orderState);
            this.update(orderUshopDO);
            return ApiResult.success();
        }

        return ApiResult.failure("订单不存在或交易被冻结");
    }

    /**
     * 根据时间和counterId查询
     *
     * @param
     * @return
     */
    @Override
    public ApiResult getDataByCustomer(Map<String, Object> param) {
        String counterId = param.get("counterId").toString();
        Object gmtCreat = param.get("gmtCreate");
        //开始时间
        Object tempStartTime = param.get("startTime");
        String startTime = tempStartTime == null ? "" : tempStartTime.toString();
        //结束时间
        Object tempEndTime = param.get("endTime");
        String endTime = tempEndTime == null ? "" : tempEndTime.toString();

        String gmtCreate = gmtCreat == null ? "" : gmtCreat.toString();
        if (StringUtils.isEmpty(counterId)) {
            return ApiResult.failure("参数不能为空");
        }
        LOGGER.info("时间为:{}，收银台ID:{}", gmtCreate, counterId);
        //格式化时间戳
        // String format = DateUtils.format(new Date(Long.valueOf(gmtCreate)),DateUtils.YMD);
        String start = DateUtils.format(new Date(Long.valueOf(startTime)), DateUtils.YMDHMS);
        String end = DateUtils.format(new Date(Long.valueOf(endTime)), DateUtils.YMDHMS);
//         Long start = Long.valueOf(startTime);
//        Long end = Long.valueOf(endTime);

        //订单状态赋值
        String orderState[] = new String[3];
        orderState[0] = OrderUshopEnumConstants.ENUM_ORDERSTATE_PAY;
        orderState[1] = OrderUshopEnumConstants.ENUM_ORDERSTATE_REFUNDED;
        orderState[2] = OrderUshopEnumConstants.ENUM_ORDERSTATE_REFUNDFAIL;

        param.put("orderState", orderState);
        //param.put("gmtCreate", format);
        param.put("startTimes", start);
        param.put("endTimes", end);
        LOGGER.info("格式化开始查询时间{}", start);
        LOGGER.info("格式化结束查询时间{}", end);
        Query query = new Query(param);

        //查询订单列表
        List<OrderUshopDTO> orderUshopDO = orderUshopDao.getDataByCustomer(query);
        
        //查询分页需要总数据量
        int total = orderUshopDao.countForOrder(query);
        //包装分页对象
        PageUtils pageUtils = new PageUtils(orderUshopDO, total);

        //查询当前时间的收入和订单完成数量
        Map<String, Object> map = new HashMap<>();
        String orderstates[] = new String[]{OrderUshopEnumConstants.ENUM_ORDERSTATE_PAY,OrderUshopEnumConstants.ENUM_ORDERSTATE_REFUNDFAIL};
        map.put("orderState", orderstates);
        map.put("counterId", counterId);
        // map.put("gmtCreate",format);
        map.put("startTimes", start);
        map.put("endTimes", end);
        BigDecimal amountMoney = orderUshopDao.countOderMoney(map);
        int countOderNumber = orderUshopDao.countOderNumber(map);

        //包装返回参数
        Map<String, Object> result = new HashMap<>();
        result.put("pageUtils", pageUtils);
        result.put("amountMoney", amountMoney);
        result.put("countOderNumber", countOderNumber);
        return ApiResult.success(result);
    }

    /**
     * 根据和counterId查询
     *
     * @param
     * @param
     * @return
     */
    @Override
    public ApiResult getOrderByCountId(Map<String, Object> param) {
        Object par = param.get("counterId");
        String counterId = par == null ? "" : par.toString();
        //String counterId = param.get("counterId").toString();
        if (StringUtils.isEmpty(counterId)) {
            return ApiResult.failure("参数不能为空");
        }
		/*param.put("orderState","1");
        param.put("orderState","3");*/
        String orderState[] = new String[3];
        orderState[0] = OrderUshopEnumConstants.ENUM_ORDERSTATE_PAY;
        orderState[1] = OrderUshopEnumConstants.ENUM_ORDERSTATE_REFUNDED;
        orderState[2] = OrderUshopEnumConstants.ENUM_ORDERSTATE_REFUNDFAIL;
        param.put("orderState", orderState);
        Query query = new Query(param);

        List<OrderUshopDTO> orderUshopDO = orderUshopDao.getDataByCustomer(query);
        Map<String, Object> map = new HashMap<>();
        String orderstates[] = new String[]{OrderUshopEnumConstants.ENUM_ORDERSTATE_PAY,OrderUshopEnumConstants.ENUM_ORDERSTATE_REFUNDFAIL};

        map.put("counterId", counterId);
        map.put("orderState", orderstates);
        BigDecimal amountMoney = orderUshopDao.countOderMoney(map);
        int countOderNumber = orderUshopDao.countOderNumber(map);
        int total = orderUshopDao.countForOrder(query);
        PageUtils pageUtils = new PageUtils(orderUshopDO, total);
        Map<String, Object> result = new HashMap<>();
        result.put("pageUtils", pageUtils);
        result.put("amountMoney", amountMoney);
        result.put("countOderNumber", countOderNumber);
        return ApiResult.success(result);


    }

    /**
     * @param
     * @return
     */
    @Override
    public ApiResult getOrderByGmtTime(Map<String, Object> param) {
        Object par = param.get("gmtCreate");
        Long time = par == null ? 0 : Long.valueOf(String.valueOf(par));
        //Long time = Long.valueOf(String.valueOf(param.get("gmtCreate")));
        String orderState[] = new String[3];
        orderState[0] = OrderUshopEnumConstants.ENUM_ORDERSTATE_PAY;
        orderState[1] = OrderUshopEnumConstants.ENUM_ORDERSTATE_REFUNDED;
        orderState[2] = OrderUshopEnumConstants.ENUM_ORDERSTATE_REFUNDFAIL;
        param.put("orderState", orderState);
        if (time == null) {
            return ApiResult.failure("参数不能为空");
        }
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String format = formatter.format(time);
        param.put("gmtCreate", format);
        LOGGER.info("传入参数" + par);
        LOGGER.info("订单筛选时间" + format);
        Query query = new Query(param);
        List<OrderUshopDTO> orderUshopDO = orderUshopDao.getDataByCustomer(query);

        int total = orderUshopDao.countForOrder(query);
        PageUtils pageUtils = new PageUtils(orderUshopDO, total);
        return ApiResult.success(pageUtils);
    }

    @Override
    public ApiResult getTodayOrder(Map<String, Object> param) {
        Object par = param.get("counterId");
        String accountId = FilterContextHandler.getUserID();
        String counterId = par == null ? "" : par.toString();
        if (StringUtils.isEmpty(counterId)) {
            ApiResult.failure("参数不能为空");
        }
        String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String gmtCreate = date + " " + "00:00:00";
        String orderState[] = new String[3];
        orderState[0] = OrderUshopEnumConstants.ENUM_ORDERSTATE_PAY;
        orderState[1] = OrderUshopEnumConstants.ENUM_ORDERSTATE_REFUNDED;
        orderState[2] = OrderUshopEnumConstants.ENUM_ORDERSTATE_REFUNDFAIL;
        param.put("orderState", orderState);
        param.put("gmtCreate", gmtCreate);
        //param.put("gmtCreate",endTime);
        int count = orderUshopDao.countOrder(param);
        return ApiResult.success(count);


    }

    @Override
    public ApiResult countOrder(Map<String, Object> param) {
        //String counterId = param.get("counterId").toString();
        Object par = param.get("counterId");
        String counterId = par == null ? "" : par.toString();
        if (StringUtils.isEmpty(counterId)) {
            ApiResult.failure("参数不能为空");
    }
        String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String gmtCreate = date + " " + "00:00:00";
        //String gmtCreate = "2019-06-06 00:00:04";
        //LOGGER.info("当前时间为："+date);
        String orderState[] = new String[3];
        orderState[0] = OrderUshopEnumConstants.ENUM_ORDERSTATE_PAY;
        orderState[1] = OrderUshopEnumConstants.ENUM_ORDERSTATE_REFUNDFAIL;
        //orderState[1]=OrderUshopEnumConstants.ENUM_ORDERSTATE_REFUNDED;
        // orderState[2] =OrderUshopEnumConstants.ENUM_ORDERSTATE_REFUNDFAIL;
        param.put("orderState", orderState);
        param.put("gmtCreate", gmtCreate);
        int count = orderUshopDao.countOrder(param);
        return ApiResult.success(count);

    }


}
